package com.usabb.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ifast.common.annotation.Log;
import com.ifast.common.base.BaseController;
import com.ifast.common.utils.Result;
import com.usabb.domain.*;
import com.usabb.enums.AmenityEnum;
import com.usabb.enums.BizTypeEnum;
import com.usabb.pojo.vo.MaternityBaseVO;
import com.usabb.service.*;
import com.usabb.utils.AmenityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <pre>
 * 月子中心基础表
 * </pre>
 * <small> 2019-08-19 18:03:27 | Aron</small>
 */
@Controller
@RequestMapping("/usabb/maternityBase")
public class MaternityBaseController extends BaseController {
    @Autowired
    private MaternityBaseService maternityBaseService;
    @Autowired
    private LabelService labelService;
    @Autowired
    private PromiseService promiseService;
    @Autowired
    private AmenityService amenityService;
    @Autowired
    private AmenityRelationService amenityRelationService;
    @Autowired
    private HospitalService hospitalService;
    @Autowired
    private HospitalRelationService hospitalRelationService;
    @Autowired
    private RoundRelationService roundRelationService;

    @GetMapping()
    @RequiresPermissions("usabb:maternityBase:maternityBase")
    String MaternityBase() {
        return "usabb/maternityBase/maternityBase";
    }

    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("usabb:maternityBase:maternityBase")
    public Result<Page<MaternityBaseDO>> list(MaternityBaseDO maternityBaseDO) {
        Wrapper<MaternityBaseDO> wrapper = new EntityWrapper<>(maternityBaseDO).orderBy("id", false);
        Page<MaternityBaseDO> page = maternityBaseService.selectPage(getPage(MaternityBaseDO.class), wrapper);
        return Result.ok(page);
    }

    @GetMapping("/add")
    @RequiresPermissions("usabb:maternityBase:add")
    String add(Model model) {
        final List<LabelDO> labels = labelService.selectList(null);
        model.addAttribute("labels", labels);

        final List<PromiseDO> promises = promiseService.selectList(null);
        model.addAttribute("promises", promises);

        return "usabb/maternityBase/add";
    }

    @GetMapping("/edit/{id}")
    @RequiresPermissions("usabb:maternityBase:edit")
    String edit(@PathVariable("id") Long id, Model model) {
        MaternityBaseDO maternityBase = maternityBaseService.selectById(id);
        model.addAttribute("maternityBase", maternityBase);
        return "usabb/maternityBase/edit";
    }

    @Log("添加月子中心基础表")
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("usabb:maternityBase:add")
    public Result<String> save(MaternityBaseVO baseVO) {
        maternityBaseService.insertVo(baseVO);
        return Result.ok();
    }

    @Log("修改月子中心基础表")
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("usabb:maternityBase:edit")
    public Result<String> update(MaternityBaseDO maternityBase) {
        boolean update = maternityBaseService.updateById(maternityBase);
        return update ? Result.ok() : Result.fail();
    }

    @Log("删除月子中心基础表")
    @PostMapping("/remove")
    @ResponseBody
    @RequiresPermissions("usabb:maternityBase:remove")
    public Result<String> remove(Long id) {
        maternityBaseService.deleteById(id);
        return Result.ok();
    }

    @Log("批量删除月子中心基础表")
    @PostMapping("/batchRemove")
    @ResponseBody
    @RequiresPermissions("usabb:maternityBase:batchRemove")
    public Result<String> remove(@RequestParam("ids[]") Long[] ids) {
        maternityBaseService.deleteBatchIds(Arrays.asList(ids));
        return Result.ok();
    }


    // =======================
    @Log("月子中心 套餐 - 服务类目")
    @GetMapping("/combos-cate/{baseId}")
    public String goCombosCategoryPage(@PathVariable("baseId") Long baseId, Model model) {
        MaternityBaseDO maternityBase = maternityBaseService.selectById(baseId);

        model.addAttribute("maternityBase", maternityBase);
        return "usabb/maternityCombosCategory/combosCategory";
    }

    @Log("月子中心 套餐管理")
    @GetMapping("/combos/{baseId}")
    public String goCombosPage(@PathVariable("baseId") Long baseId, Model model) {
        MaternityBaseDO maternityBase = maternityBaseService.selectById(baseId);

        model.addAttribute("maternityBase", maternityBase);
        return "usabb/maternityCombos/maternityCombos";
    }

    @GetMapping("/{baseId}/attach")
    public String goAttachPage(@PathVariable("baseId") Long baseId, Model model) {
        MaternityBaseDO maternityBase = maternityBaseService.selectById(baseId);

        model.addAttribute("maternityBase", maternityBase);
        return "usabb/attachCenter/attach";
    }

    @Log("月子中心 配套&周边")
    @GetMapping("/amenity/{baseId}")
    public String goAmenityPage(@PathVariable("baseId") Long baseId, Model model) {
        MaternityBaseDO maternityBase = maternityBaseService.selectById(baseId);

        model.addAttribute("maternityBase", maternityBase);

        final List<AmenityDO> amenityDOS = amenityService.selectList(null);
        AmenityUtils.append(model, amenityDOS);

        //checked items
        List<Long> relationAmenityIds = amenityRelationService.getAmenityIdListBy(BizTypeEnum.CENTER.name(), baseId);
        model.addAttribute("checkedAmenityIds", relationAmenityIds);

        //round
        final List<RoundRelationDO> roundRelationDOS =
                roundRelationService.getListBy(BizTypeEnum.CENTER.name(), baseId);
        model.addAttribute("roundRelationDOS", roundRelationDOS);

        //hospital
        List<HospitalDO> hospitalDOS = hospitalService.selectList(null);
        model.addAttribute("hospitalDOS", hospitalDOS);
        //checked hospitals
        final List<Long> hospitalIds = hospitalRelationService.getHospitalIdsBy(BizTypeEnum.CENTER.name(), baseId);
        model.addAttribute("checkedHospitalIds", hospitalIds);

        return "usabb/amenityCenter/add";
    }


    @Log("月子中心 客人感言")
    @GetMapping("/guest/{baseId}")
    public String goGuestPage(@PathVariable("baseId") Long baseId, Model model) {
        MaternityBaseDO maternityBase = maternityBaseService.selectById(baseId);

        model.addAttribute("maternityBase", maternityBase);
        return "usabb/guestCase/guestCase";
    }



}
